/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Alvaro
 */
public class order {

    private int id;
    String customeremail;
    String customername;
    String customersecondname;
    String customercountry;
    String customercity;
    String customerstreet;
    String customernumber;
    String customerfloor;
    String customerletter;
    float price;
    String paymentmethod;
    String paymentdate;
    String product;
    float qty;

    public String getCustomercity() {
        return customercity;
    }

    public void setCustomercity(String customercity) {
        this.customercity = customercity;
    }

    public String getCustomercountry() {
        return customercountry;
    }

    public void setCustomercountry(String customercountry) {
        this.customercountry = customercountry;
    }

    public String getCustomeremail() {
        return customeremail;
    }

    public void setCustomeremail(String customeremail) {
        this.customeremail = customeremail;
    }

    public String getCustomerfloor() {
        return customerfloor;
    }

    public void setCustomerfloor(String customerfloor) {
        this.customerfloor = customerfloor;
    }

    public String getCustomerletter() {
        return customerletter;
    }

    public void setCustomerletter(String customerletter) {
        this.customerletter = customerletter;
    }

    public String getCustomername() {
        return customername;
    }

    public void setCustomername(String customername) {
        this.customername = customername;
    }

    public String getCustomernumber() {
        return customernumber;
    }

    public void setCustomernumber(String customernumber) {
        this.customernumber = customernumber;
    }

    public String getCustomersecondname() {
        return customersecondname;
    }

    public void setCustomersecondname(String customersecondname) {
        this.customersecondname = customersecondname;
    }

    public String getCustomerstreet() {
        return customerstreet;
    }

    public void setCustomerstreet(String customerstreet) {
        this.customerstreet = customerstreet;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPaymentdate() {
        return paymentdate;
    }

    public void setPaymentdate(String paymentdate) {
        this.paymentdate = paymentdate;
    }

    public String getPaymentmethod() {
        return paymentmethod;
    }

    public void setPaymentmethod(String paymentmethod) {
        this.paymentmethod = paymentmethod;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public String getProduct() {
        return product;
    }

    public void setProduct(String product) {
        this.product = product;
    }

    public float getQty() {
        return qty;
    }

    public void setQty(float qty) {
        this.qty = qty;
    }

    public order(String customeremail, String customername, String customersecondname, String customercountry, String customercity, String customerstreet, String customernumber, String customerfloor, String customerletter, float price, String paymentmethod, String paymentdate, String product, float qty) {
        this.customeremail = customeremail;
        this.customername = customername;
        this.customersecondname = customersecondname;
        this.customercountry = customercountry;
        this.customercity = customercity;
        this.customerstreet = customerstreet;
        this.customernumber = customernumber;
        this.customerfloor = customerfloor;
        this.customerletter = customerletter;
        this.price = price;
        this.paymentmethod = paymentmethod;
        this.paymentdate = paymentdate;
        this.product = product;
        this.qty = qty;
    }

    public order() {
    }

    /**
     * ************ DB *************************
     */
    public boolean guardar() throws SQLException {

        ObjetoDB o = new ObjetoDB();

        String price2 = Float.toString(price);
        String qty2 = Float.toString(qty);

        o.setTabla("order");
        o.addCampo("customeremail", customeremail);
        o.addCampo("customername", customername);
        o.addCampo("customersecondname", customersecondname);
        o.addCampo("customercountry", customercountry);
        o.addCampo("customercity", customercity);
        o.addCampo("customerstreet", customerstreet);
        o.addCampo("customernumber", customernumber);
        o.addCampo("customerfloor", customerfloor);
        o.addCampo("customerletter", customerletter);
        o.addCampo("price", price2);
        o.addCampo("paymentmethod", paymentmethod);
        o.addCampo("paymentdate", paymentdate);
        o.addCampo("product", product);
        o.addCampo("qty", qty2);
        AdminDB adb = new AdminDB();
        return adb.guardar(o);
    }

    public void getByEmail(Connection conn, String email) throws SQLException {
        ResultSet rset = null;
        try {
            PreparedStatement stmt = null;

            if (null == stmt) {
                stmt = conn.prepareStatement("SELECT * FROM `F1shopOnline`.`order` WHERE customeremail='" + email + "';");
                System.out.println("Busqueda de pedidos por email ...");
            }
            rset = stmt.executeQuery();
            if (rset.next()) {

                this.setId(rset.getInt("ID"));
                this.customeremail = rset.getString("customeremail");
                this.customername = rset.getString("customername");
                this.customersecondname = rset.getString("customersecondname");
                this.customercountry = rset.getString("customercountry");
                this.customercity = rset.getString("customercity");
                this.customerstreet = rset.getString("customerstreet");
                this.customernumber = rset.getString("customernumber");
                this.customerfloor = rset.getString("customerfloor");
                this.customerletter = rset.getString("customerletter");
                this.price = rset.getFloat("price");
                this.paymentmethod = rset.getString("paymentmethod");
                this.paymentdate = rset.getString("paymentdate");
                this.product = rset.getString("product");
                this.qty = rset.getInt("qty");
            }
            if (stmt != null) {
                stmt.close();
            }

        } catch (SQLException e) {
            System.out.println("Error de MySQL: " + e.getMessage());
        } catch (Exception e) {
            Logger.getLogger("Recuperar pedido").log(Level.SEVERE, "ERROR: " + e.getLocalizedMessage());
        }
        finally {
            if (rset != null) {
                try {
                    rset.close();
                } catch (SQLException e) {
                    Logger.getLogger("GetByEmail").log(Level.SEVERE, "ERROR AL CERRAR RSET: " + e.getLocalizedMessage());
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    Logger.getLogger("GetByEmail").log(Level.SEVERE, "ERROR AL CERRAR LA CONEXION: " + e.getLocalizedMessage());
                }
            }
        }
    }

    public boolean borrar(String id) throws SQLException {
        AdminDB adb = new AdminDB();
        int cpID = adb.getID("order", "id='" + id + "'");
        ObjetoDB o = new ObjetoDB();
        o.setTabla("order");
        o.addCampo("id", id);
        return adb.borrar(o);
    }

    public boolean borrarTodosPedidos() throws SQLException {
        AdminDB adb = new AdminDB();
        ObjetoDB o = new ObjetoDB();
        o.setTabla("order");
        return adb.vaciarTabla(o);
    }

    public List<order> getAllPedidos(Connection conn) throws SQLException {
        ResultSet rset = null;
        try {
            PreparedStatement stmt = null;

            if (null == stmt) {
                stmt = conn.prepareStatement("SELECT * FROM `F1shopOnline`.`order`");
                System.out.println("Listado de todos los pedidos ...");
            }
            rset = stmt.executeQuery();

            List<order> ord = new ArrayList<order>();

            while (rset.next()) {
                order pedido = new order();
                pedido.setId(rset.getInt("ID"));
                pedido.setCustomeremail(rset.getString("customeremail"));
                pedido.setCustomername(rset.getString("customername"));
                pedido.setCustomersecondname(rset.getString("customersecondname"));
                pedido.setCustomercountry(rset.getString("customercountry"));
                pedido.setCustomercity(rset.getString("customercity"));
                pedido.setCustomerstreet(rset.getString("customerstreet"));
                pedido.setCustomernumber(rset.getString("customernumber"));
                pedido.setCustomerfloor(rset.getString("customerfloor"));
                pedido.setCustomerletter(rset.getString("customerletter"));
                pedido.setPrice(rset.getFloat("price"));
                pedido.setPaymentmethod(rset.getString("paymentmethod"));
                pedido.setPaymentdate(rset.getString("paymentdate"));
                pedido.setQty(rset.getInt("qty"));

                ord.add(pedido);
            }
            if (stmt != null) {
                stmt.close();
            }
            
            return ord;
        } catch (SQLException e) {
            System.out.println("Error de MySQL: " + e.getMessage());
            return null;
        } catch (Exception e) {
            Logger.getLogger("Pedido").log(Level.SEVERE, "ERROR: " + e.getLocalizedMessage());
            return null;
        }
        finally {
            if (rset != null) {
                try {
                    rset.close();
                } catch (SQLException e) {
                    Logger.getLogger("GetAllPedidos").log(Level.SEVERE, "ERROR AL CERRAR RSET: " + e.getLocalizedMessage());
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    Logger.getLogger("GetAllPedidos").log(Level.SEVERE, "ERROR AL CERRAR LA CONEXION: " + e.getLocalizedMessage());
                }
            }
        }
    }

    public List<order> searchPedidos(Connection conn, String busqueda, String by) throws SQLException {
        ResultSet rset = null;
        try {
            PreparedStatement stmt = null;

            if (null == stmt) {
                stmt = conn.prepareStatement("SELECT * FROM `F1shopOnline`.`order` WHERE " + by + " LIKE '%" + busqueda + "%'");
                System.out.println("Listado de todos los pedidos ...");
            }
            rset = stmt.executeQuery();
            List<order> ord = new ArrayList<order>();
            while (rset.next()) {
                order pedido = new order();
                pedido.setId(rset.getInt("ID"));
                pedido.setCustomeremail(rset.getString("customeremail"));
                pedido.setCustomername(rset.getString("customername"));
                pedido.setCustomersecondname(rset.getString("customersecondname"));
                pedido.setCustomercountry(rset.getString("customercountry"));
                pedido.setCustomercity(rset.getString("customercity"));
                pedido.setCustomerstreet(rset.getString("customerstreet"));
                pedido.setCustomernumber(rset.getString("customernumber"));
                pedido.setCustomerfloor(rset.getString("customerfloor"));
                pedido.setCustomerletter(rset.getString("customerletter"));
                pedido.setPrice(rset.getFloat("price"));
                pedido.setPaymentmethod(rset.getString("paymentmethod"));
                pedido.setPaymentdate(rset.getString("paymentdate"));
                pedido.setProduct(rset.getString("product"));
                pedido.setQty(rset.getInt("qty"));

                ord.add(pedido);
            }

            if (stmt != null) {
                stmt.close();
            }
            
            return ord;
        } catch (SQLException e) {
            System.out.println("Error de MySQL: " + e.getMessage());
            return null;
        } catch (Exception e) {
            Logger.getLogger("Pedido").log(Level.SEVERE, "ERROR: " + e.getLocalizedMessage());
            return null;
        }
        finally {
            if (rset != null) {
                try {
                    rset.close();
                } catch (SQLException e) {
                    Logger.getLogger("SearchPedidos").log(Level.SEVERE, "ERROR AL CERRAR RSET: " + e.getLocalizedMessage());
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    Logger.getLogger("SearchPedidos").log(Level.SEVERE, "ERROR AL CERRAR LA CONEXION: " + e.getLocalizedMessage());
                }
            }
        }
    }

    public List<order> searchPedidosDeUsuario(Connection conn, String email, String by, String busqueda, int option) throws SQLException {
        ResultSet rset = null;
        try {
            PreparedStatement stmt = null;

            if (null == stmt && option == 1) {
                stmt = conn.prepareStatement("SELECT * FROM `F1shopOnline`.`order` WHERE customeremail='" + email + "';");
            } else {
                if (null == stmt && by != null && busqueda != null) {
                    stmt = conn.prepareStatement("SELECT * FROM `F1shopOnline`.`order` WHERE customeremail='" + email + "' AND " + by + "='" + busqueda + "'");
                } else {
                    stmt = conn.prepareStatement("SELECT * FROM `F1shopOnline`.`order`;");
                }
            }
            rset = stmt.executeQuery();
            List<order> ord = new ArrayList<order>();
            while (rset.next()) {
                order pedido = new order();
                pedido.setId(rset.getInt("ID"));
                pedido.setCustomeremail(rset.getString("customeremail"));
                pedido.setCustomername(rset.getString("customername"));
                pedido.setCustomersecondname(rset.getString("customersecondname"));
                pedido.setCustomercountry(rset.getString("customercountry"));
                pedido.setCustomercity(rset.getString("customercity"));
                pedido.setCustomerstreet(rset.getString("customerstreet"));
                pedido.setCustomernumber(rset.getString("customernumber"));
                pedido.setCustomerfloor(rset.getString("customerfloor"));
                pedido.setCustomerletter(rset.getString("customerletter"));
                pedido.setPrice(rset.getFloat("price"));
                pedido.setPaymentmethod(rset.getString("paymentmethod"));
                pedido.setPaymentdate(rset.getString("paymentdate"));
                pedido.setProduct(rset.getString("product"));
                pedido.setQty(rset.getInt("qty"));

                ord.add(pedido);
            }
            if (stmt != null) {
                stmt.close();
            }
            

            return ord;

        } catch (SQLException e) {
            System.out.println("Error de MySQL: " + e.getMessage());
            return null;
        } catch (Exception e) {
            Logger.getLogger("Pedido").log(Level.SEVERE, "ERROR: " + e.getLocalizedMessage());
            return null;
        }
        finally {
            if (rset != null) {
                try {
                    rset.close();
                } catch (SQLException e) {
                    Logger.getLogger("SearchPedidosdeUsuario").log(Level.SEVERE, "ERROR AL CERRAR RSET: " + e.getLocalizedMessage());
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    Logger.getLogger("SearchPedidosdeUsuario").log(Level.SEVERE, "ERROR AL CERRAR LA CONEXION: " + e.getLocalizedMessage());
                }
            }
        }
    }

    public void paymentMethod(Connection conn, String paymentMethod, String email, String product) throws SQLException {
        try {
            PreparedStatement stmt = null;

            if (null == stmt) {
                stmt = conn.prepareStatement("UPDATE `F1ShopOnline`.`order` SET paymentmethod='" + paymentMethod + "' WHERE customeremail='" + email + "' AND product='" + product + "'");
            }
            stmt.executeUpdate();
            stmt.close();
        } catch (SQLException e) {
            System.out.println("Error de MySQL: " + e.getMessage());
        } catch (Exception e) {
            Logger.getLogger("PaymentMethod").log(Level.SEVERE, "ERROR: " + e.getLocalizedMessage());
            System.out.println("Error inesperado: " + e.getMessage());
        }
        finally {
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    Logger.getLogger("RecoverName").log(Level.SEVERE, "ERROR AL CERRAR LA CONEXION: " + e.getLocalizedMessage());
                }
            }
        }
    }
}
